package com.eques.util

import android.content.Context
import android.content.SharedPreferences

/**
 * 保存其它共享数据
 */
class EquesPreference(ctx: Context) {
    val instance: SharedPreferences

    init {
        instance = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
    }

    val editor: SharedPreferences.Editor
        get() = instance.edit()

    fun putString(key: String?, value: String?) {
        editor.putString(key, value).commit()
    }

    fun putBoolean(key: String?, value: Boolean) {
        editor.putBoolean(key, value).commit()
    }

    fun getBoolean(key: String?): Boolean {
        return instance.getBoolean(key, false)
    }

    fun getBoolean(key: String?, defValue: Boolean): Boolean {
        return instance.getBoolean(key, defValue)
    }

    fun putInt(key: String?, value: Int) {
        editor.putInt(key, value).commit()
    }

    fun putLong(key: String?, value: Long) {
        editor.putLong(key, value).commit()
    }

    fun putFloat(key: String?, value: Float) {
        editor.putFloat(key, value).commit()
    }

    fun remove(key: String?) {
        editor.remove(key).commit()
    }

    fun getString(key: String?): String? {
        return instance.getString(key,"")
    }

    fun getInt(key: String?): Int {
        return instance.getInt(key, 0)
    }

    fun getInt2(key: String?, defValue: Int): Int {
        return instance.getInt(key, defValue)
    }

    fun getLong(key: String?): Long {
        return instance.getLong(key, -1)
    }

    fun getFloat(key: String?): Float {
        return instance.getFloat(key, 0.0f)
    }

    fun getString(key: String?, default_value: String?): String? {
        return instance.getString(key, default_value)
    }

    fun getMainUrl(): String? {
        return instance.getString(Config.MAIN_ADDRESS, "")
    }

    fun getSubUrl(): String? {
        return instance.getString(Config.SUB_ADDRESS, "")
    }

    companion object {
        private const val PREFS_NAME = "eques_prefs"
    }
}
